package com.base.cn.platform.os.controller.mobile.op.coupon;

import com.base.cn.platform.os.common.MobileBaseController;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.op.coupon.OpCouponShop;
import com.base.cn.platform.os.service.manage.op.coupon.OpCouponCodeService;
import com.base.cn.platform.os.service.manage.op.coupon.OpCouponService;
import com.base.cn.platform.os.service.manage.op.coupon.OpCouponShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * H5优惠券controller
 *
 * @author zsw
 * @create 2018-06-15
 */
@Controller
@RequestMapping("/mobile/uc/op/coupon")
public class MobileCouponController extends MobileBaseController {
    @Autowired
    private OpCouponCodeService couponCodeService;
    @Autowired
    private OpCouponService opCouponService;
    @Autowired
    private OpCouponShopService opCouponShopService;

    /**
     * 我的优惠券列表
     * @param request
     * @return
     */
    @RequestMapping("/myCoupons")
    public String myCoupons(HttpServletRequest request){
        return resultPage("/op/coupon/u-coupon");
    }


    /**
     * 我的优惠券
     * @param request
     * @return
     */
    @RequestMapping("/myAjaxCoupon")
    public String myAjaxCoupon(HttpServletRequest request,
                                Model model,
                               @RequestParam(value = "currentPage", required = false, defaultValue = "1") int currentPage){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        HashMap<String, Object> couponCode = new HashMap<>();
        couponCode.put("userId",userId);
        Map<String, Object> couponCodeListPage = couponCodeService.queryOpCouponCodeListPage(couponCode, currentPage, true, true);
        model.addAttribute("dataMap",couponCodeListPage);
        return resultPage("/op/coupon/u-ajax-coupon");
    }

    /**
     * 用户领取优惠券
     * @param request HttpServletRequest
     * @param couponIds 优惠券ID串
     * @return Map<String,Object>
     */
    @RequestMapping("/couponReceive")
    @ResponseBody
    public Map<String,Object> couponReceive(HttpServletRequest request,
                                            @RequestParam("couponIds") String couponIds){
        //获取用户ID
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        return opCouponService.couponReceive(couponIds,userId);
    }

    /**
     * 获取优惠券关联的商品数据
     * @param couponId 优惠券ID
     * @return Map<String,Object>
     */
    @RequestMapping("/showCouponShopData")
    @ResponseBody
    public Map<String,Object> showCouponShopData(@RequestParam("couponId") BigDecimal couponId){
        List<OpCouponShop> couponShopList = opCouponShopService.showCouponShopData(couponId);
        return ResultUtil.SUCCESS(couponShopList);
    }

    /**
     * 获取针对用户可以领取的优惠券
     * @param request
     * @return Map<String,Object>
     */
    @RequestMapping("/findForUsersCouponList")
    @ResponseBody
    public Map<String,Object> findForUsersCouponList(HttpServletRequest request){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        return opCouponService.findForUsersCouponList(userId);
    }

}
